﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;
using YArchitech.Controls;
using YArchitech.LIB;
using YArchitech.LIB.YJKControls;

namespace YArchitech.Plumbing
{
	public partial class FormPumpProduct : System.Windows.Forms.Form
	{
		private void FormPumpProduct_Load(object sender, EventArgs e)
		{
			this.InitTreeView();
			this.InitDataGridViewColumn();
			this.InitFourCombox();
			this.InitComboBoxLine();
			this.checkBoxPoint.Checked = true;
			this.checkBoxTitle.Checked = true;
		}

		private void treeViewPump_BeforeSelect(object sender, TreeViewCancelEventArgs e)
		{
			if (this.treeViewPump.SelectedNode != null)
			{
				this.treeViewPump.SelectedNode.BackColor = Color.White;
				this.treeViewPump.SelectedNode.ForeColor = Color.Black;
			}
		}

		private void treeViewPump_AfterSelect(object sender, TreeViewEventArgs e)
		{
			this.treeViewPump.SelectedNode.BackColor = Color.DodgerBlue;
			this.treeViewPump.SelectedNode.ForeColor = Color.White;
			if (e.Node.Parent == null)
			{
				this.GetListPumpDatasByAllFactory();
			}
			else if (e.Node.Nodes == null || e.Node.Nodes.Count < 1)
			{
				this.GetListPumpDatasBySelectTypeInFactory();
			}
			else
			{
				this.GetListPumpDatasBySelectFactory();
			}
			this.dataGridView.Rows.Clear();
			this.UpdateDataGridViewDatas(this.GetFiterConditionsFromCombox());
		}

		private void btnCancle_Click(object sender, EventArgs e)
		{
			base.Close();
		}

		private void btnLookUp_Click(object sender, EventArgs e)
		{
			new FormSeeThePump(this.pumpdata).ShowDialog();
		}

		private void dataGridView_SelectionChanged(object sender, EventArgs e)
		{
			if (this.dataGridView != null && this.dataGridView.SelectedRows.Count == 1)
			{
				this.pumpdata = new HYPumpData();
				this.pumpdata.StrSpecification = this.dataGridView.SelectedRows[0].Cells[0].Value.ToString();
				this.pumpdata.StrFactory = this.dataGridView.SelectedRows[0].Cells[9].Value.ToString();
				this.pumpdata.StrTypeInFactory = this.dataGridView.SelectedRows[0].Cells[10].Value.ToString();
				if (HYPumpDataManager.GetSpecPumpData(ref this.pumpdata))
				{
					this.UpdataChartData();
					this.InitPictureBox();
				}
				this.btnLookUp.Enabled = true;
				return;
			}
			this.btnLookUp.Enabled = false;
		}

		private void cbBasicType_SelectedIndexChanged(object sender, EventArgs e)
		{
			this.UpdateDataGridViewDatas(this.GetFiterConditionsFromCombox());
		}

		private void cbStructureType_SelectedIndexChanged(object sender, EventArgs e)
		{
			this.UpdateDataGridViewDatas(this.GetFiterConditionsFromCombox());
		}

		private void cbInstallForms_SelectedIndexChanged(object sender, EventArgs e)
		{
			this.UpdateDataGridViewDatas(this.GetFiterConditionsFromCombox());
		}

		private void cbPurpose_SelectedIndexChanged(object sender, EventArgs e)
		{
			this.UpdateDataGridViewDatas(this.GetFiterConditionsFromCombox());
		}

		private void comboBoxLine_SelectedIndexChanged(object sender, EventArgs e)
		{
			this.UpdataChartData();
		}

		private void checkBoxPoint_CheckedChanged(object sender, EventArgs e)
		{
			this.hyPumpGraphChart.SetMarker(this.checkBoxPoint.Checked);
		}

		private void checkBoxTitle_CheckedChanged(object sender, EventArgs e)
		{
			this.hyPumpGraphChart.ShowXYTitle(this.checkBoxTitle.Checked);
		}

		private void btnSelPump_Click(object sender, EventArgs e)
		{
			if (this.dataGridView.SelectedRows.Count != 0)
			{
				foreach (object obj in this.dataGridView.SelectedRows)
				{
					DataGridViewRow dataGridViewRow = (DataGridViewRow)obj;
					this.pumpdata = new HYPumpData();
					this.pumpdata.StrSpecification = dataGridViewRow.Cells[0].Value.ToString();
					this.pumpdata.StrFactory = dataGridViewRow.Cells[9].Value.ToString();
					this.pumpdata.StrTypeInFactory = dataGridViewRow.Cells[10].Value.ToString();
					HYPumpDataManager.GetSpecPumpData(ref this.pumpdata);
					this.m_arrSelPumps.Add(this.pumpdata);
				}
			}
			YJKMessageBox.Information("添加水泵成功！");
		}

		public FormPumpProduct()
		{
			this.InitializeComponent();
			base.Icon = Resource.YArchitechIcon;
			this.hyPumpGraphChart.InitSeries();
		}

		public FormPumpProduct(string formText)
		{
			this.InitializeComponent();
			base.Icon = Resource.YArchitechIcon;
			this.Text = formText;
			this.groupBox3.Visible = false;
			this.groupBox4.Visible = false;
			this.groupBox2.Size = new Size(811, 617);
			this.dataGridView.Size = new Size(799, 568);
			this.btnSelPump.Visible = true;
			this.dataGridView.MultiSelect = true;
			this.m_arrSelPumps = new List<HYPumpData>();
		}

		private void InitPictureBox()
		{
			if (File.Exists(this.pumpdata.strImage2D))
			{
				this.pictureBox.Image = new Bitmap(this.pumpdata.strImage2D);
				return;
			}
			this.pictureBox.Image = null;
		}

		private void InitTreeView()
		{
			this.listPumpFac = HYPumpDataManager.GetAllFactory();
			List<string> list = new List<string>();
			if (this.listPumpFac.Count > 0)
			{
				foreach (HYPumpFactory hypumpFactory in this.listPumpFac)
				{
					list.Add(hypumpFactory.szFactory);
				}
				list = list.Distinct<string>().ToList<string>();
				TreeNode treeNode = new TreeNode();
				treeNode.Text = "水泵";
				this.treeViewPump.Nodes.Add(treeNode);
				foreach (string text in list)
				{
					TreeNode treeNode2 = new TreeNode();
					treeNode2.Text = text.Trim();
					treeNode.Nodes.Add(treeNode2);
					foreach (HYPumpFactory hypumpFactory2 in this.listPumpFac)
					{
						if (text == hypumpFactory2.szFactory)
						{
							TreeNode treeNode3 = new TreeNode();
							treeNode3.Text = hypumpFactory2.szTypeInFactory;
							treeNode2.Nodes.Add(treeNode3);
						}
					}
				}
			}
			this.treeViewPump.SelectedNode = this.treeViewPump.Nodes[0];
			this.treeViewPump.ExpandAll();
		}

		private void InitFourCombox()
		{
			this.cbBasicType.Items.Add("离心式");
			this.cbBasicType.SelectedIndex = 0;
			this.cbStructureType.Items.Add("--*全部*--");
			this.cbStructureType.Items.Add("分段式多级");
			this.cbStructureType.Items.Add("单级单吸");
			this.cbStructureType.Items.Add("单级双吸");
			this.cbStructureType.Items.Add("管道泵");
			this.cbStructureType.SelectedIndex = 0;
			this.cbInstallForms.Items.Add("--*全部*--");
			this.cbInstallForms.Items.Add("立式");
			this.cbInstallForms.Items.Add("卧式");
			this.cbInstallForms.SelectedIndex = 0;
			this.cbPurpose.Items.Add("--*全部*--");
			this.cbPurpose.Items.Add("通用清水泵");
			this.cbPurpose.Items.Add("高压清水泵");
			this.cbPurpose.Items.Add("高压热水泵");
			this.cbPurpose.Items.Add("排污泵");
			this.cbPurpose.SelectedIndex = 0;
		}

		private void InitDataGridViewColumn()
		{
			DataGridViewTextBoxColumn dataGridViewTextBoxColumn = new DataGridViewTextBoxColumn();
			dataGridViewTextBoxColumn.HeaderText = "型号";
			dataGridViewTextBoxColumn.Width = 110;
			dataGridViewTextBoxColumn.SortMode = DataGridViewColumnSortMode.Automatic;
			dataGridViewTextBoxColumn.Resizable = DataGridViewTriState.True;
			dataGridViewTextBoxColumn.ReadOnly = true;
			this.dataGridView.Columns.Add(dataGridViewTextBoxColumn);
			DataGridViewTextBoxColumn dataGridViewTextBoxColumn2 = new DataGridViewTextBoxColumn();
			dataGridViewTextBoxColumn2.HeaderText = "基本类型";
			dataGridViewTextBoxColumn2.Width = 90;
			dataGridViewTextBoxColumn2.SortMode = DataGridViewColumnSortMode.Automatic;
			dataGridViewTextBoxColumn2.ReadOnly = true;
			this.dataGridView.Columns.Add(dataGridViewTextBoxColumn2);
			DataGridViewTextBoxColumn dataGridViewTextBoxColumn3 = new DataGridViewTextBoxColumn();
			dataGridViewTextBoxColumn3.HeaderText = "结构类型";
			dataGridViewTextBoxColumn3.Width = 90;
			dataGridViewTextBoxColumn3.SortMode = DataGridViewColumnSortMode.Automatic;
			dataGridViewTextBoxColumn3.ReadOnly = true;
			this.dataGridView.Columns.Add(dataGridViewTextBoxColumn3);
			DataGridViewTextBoxColumn dataGridViewTextBoxColumn4 = new DataGridViewTextBoxColumn();
			dataGridViewTextBoxColumn4.HeaderText = "安装形式";
			dataGridViewTextBoxColumn4.Width = 90;
			dataGridViewTextBoxColumn4.SortMode = DataGridViewColumnSortMode.Automatic;
			dataGridViewTextBoxColumn4.ReadOnly = true;
			this.dataGridView.Columns.Add(dataGridViewTextBoxColumn4);
			DataGridViewTextBoxColumn dataGridViewTextBoxColumn5 = new DataGridViewTextBoxColumn();
			dataGridViewTextBoxColumn5.HeaderText = "流量(m3/h)";
			dataGridViewTextBoxColumn5.Width = 90;
			dataGridViewTextBoxColumn5.SortMode = DataGridViewColumnSortMode.Automatic;
			dataGridViewTextBoxColumn5.ReadOnly = true;
			this.dataGridView.Columns.Add(dataGridViewTextBoxColumn5);
			DataGridViewTextBoxColumn dataGridViewTextBoxColumn6 = new DataGridViewTextBoxColumn();
			dataGridViewTextBoxColumn6.HeaderText = "扬程(m)";
			dataGridViewTextBoxColumn6.Width = 80;
			dataGridViewTextBoxColumn6.SortMode = DataGridViewColumnSortMode.Automatic;
			dataGridViewTextBoxColumn6.ReadOnly = true;
			this.dataGridView.Columns.Add(dataGridViewTextBoxColumn6);
			DataGridViewTextBoxColumn dataGridViewTextBoxColumn7 = new DataGridViewTextBoxColumn();
			dataGridViewTextBoxColumn7.HeaderText = "效率(%)";
			dataGridViewTextBoxColumn7.Width = 80;
			dataGridViewTextBoxColumn7.SortMode = DataGridViewColumnSortMode.Automatic;
			dataGridViewTextBoxColumn7.ReadOnly = true;
			this.dataGridView.Columns.Add(dataGridViewTextBoxColumn7);
			DataGridViewTextBoxColumn dataGridViewTextBoxColumn8 = new DataGridViewTextBoxColumn();
			dataGridViewTextBoxColumn8.HeaderText = "轴功率(kw)";
			dataGridViewTextBoxColumn8.Width = 80;
			dataGridViewTextBoxColumn8.SortMode = DataGridViewColumnSortMode.Automatic;
			dataGridViewTextBoxColumn8.ReadOnly = true;
			this.dataGridView.Columns.Add(dataGridViewTextBoxColumn8);
			DataGridViewTextBoxColumn dataGridViewTextBoxColumn9 = new DataGridViewTextBoxColumn();
			dataGridViewTextBoxColumn9.HeaderText = "用途";
			dataGridViewTextBoxColumn9.Width = 90;
			dataGridViewTextBoxColumn9.SortMode = DataGridViewColumnSortMode.Automatic;
			dataGridViewTextBoxColumn9.ReadOnly = true;
			this.dataGridView.Columns.Add(dataGridViewTextBoxColumn9);
			DataGridViewTextBoxColumn dataGridViewTextBoxColumn10 = new DataGridViewTextBoxColumn();
			dataGridViewTextBoxColumn10.HeaderText = "厂家";
			dataGridViewTextBoxColumn10.Width = 90;
			dataGridViewTextBoxColumn10.SortMode = DataGridViewColumnSortMode.Automatic;
			dataGridViewTextBoxColumn10.ReadOnly = true;
			dataGridViewTextBoxColumn10.Visible = false;
			this.dataGridView.Columns.Add(dataGridViewTextBoxColumn10);
			DataGridViewTextBoxColumn dataGridViewTextBoxColumn11 = new DataGridViewTextBoxColumn();
			dataGridViewTextBoxColumn11.HeaderText = "厂家系列类型";
			dataGridViewTextBoxColumn11.Width = 90;
			dataGridViewTextBoxColumn11.SortMode = DataGridViewColumnSortMode.Automatic;
			dataGridViewTextBoxColumn11.ReadOnly = true;
			dataGridViewTextBoxColumn11.Visible = false;
			this.dataGridView.Columns.Add(dataGridViewTextBoxColumn11);
		}

		private void GetListPumpDatasBySelectFactory()
		{
			this.listPumpData.Clear();
			List<HYPumpFactory> list = new List<HYPumpFactory>();
			foreach (HYPumpFactory hypumpFactory in this.listPumpFac)
			{
				if (hypumpFactory.szFactory == this.treeViewPump.SelectedNode.Text)
				{
					list.Add(hypumpFactory);
				}
			}
			if (list.Count > 0)
			{
				foreach (HYPumpFactory pumpFactory in list)
				{
					if (HYPumpDataManager.GetTablePumpBasicData(pumpFactory) != null && HYPumpDataManager.GetTablePumpBasicData(pumpFactory).Count > 0)
					{
						this.listPumpData.AddRange(HYPumpDataManager.GetTablePumpBasicData(pumpFactory));
					}
				}
			}
		}

		private void GetListPumpDatasBySelectTypeInFactory()
		{
			this.listPumpData.Clear();
			foreach (HYPumpFactory hypumpFactory in this.listPumpFac)
			{
				if (hypumpFactory.szTypeInFactory == this.treeViewPump.SelectedNode.Text && HYPumpDataManager.GetTablePumpBasicData(hypumpFactory) != null && HYPumpDataManager.GetTablePumpBasicData(hypumpFactory).Count > 0)
				{
					this.listPumpData.AddRange(HYPumpDataManager.GetTablePumpBasicData(hypumpFactory));
				}
			}
		}

		private void GetListPumpDatasByAllFactory()
		{
			this.listPumpData.Clear();
			if (this.listPumpFac.Count > 0)
			{
				foreach (HYPumpFactory pumpFactory in this.listPumpFac)
				{
					if (HYPumpDataManager.GetTablePumpBasicData(pumpFactory) != null && HYPumpDataManager.GetTablePumpBasicData(pumpFactory).Count > 0)
					{
						this.listPumpData.AddRange(HYPumpDataManager.GetTablePumpBasicData(pumpFactory));
					}
				}
			}
		}

		private void UpdateDataGridViewDatas(string[] conditions)
		{
			if (this.dataGridView.Rows.Count > 1)
			{
				this.dataGridView.Rows.Clear();
			}
			List<HYPumpData> list = this.FiterByConditions(conditions[0], conditions[1], conditions[2], conditions[3]);
			if (list.Count > 0)
			{
				foreach (HYPumpData hypumpData in list)
				{
					object[] values = new object[]
					{
						hypumpData.StrSpecification,
						hypumpData.StrBaseType,
						hypumpData.StrStructType,
						hypumpData.StrFitType,
						hypumpData.DQ,
						hypumpData.DH,
						hypumpData.DEffection,
						hypumpData.DPumpPower,
						hypumpData.StrUse,
						hypumpData.StrFactory,
						hypumpData.StrTypeInFactory
					};
					this.dataGridView.Rows.Add(values);
				}
				this.dataGridView.Rows[0].Selected = true;
			}
		}

		private List<HYPumpData> FiterByConditions(string baseType, string structType, string fitType, string use)
		{
			List<HYPumpData> list = new List<HYPumpData>();
			foreach (HYPumpData hypumpData in this.listPumpData)
			{
				if (hypumpData.StrBaseType == baseType && (hypumpData.StrStructType == structType || structType == "--*全部*--") && (hypumpData.StrFitType == fitType || fitType == "--*全部*--") && (hypumpData.StrUse == use || use == "--*全部*--"))
				{
					list.Add(hypumpData);
				}
			}
			return list;
		}

		private string[] GetFiterConditionsFromCombox()
		{
			return new string[]
			{
				this.cbBasicType.Text.ToString(),
				this.cbStructureType.Text.ToString(),
				this.cbInstallForms.Text.ToString(),
				this.cbPurpose.Text.ToString()
			};
		}

		private void InitComboBoxLine()
		{
			this.comboBoxLine.Items.Clear();
			this.comboBoxLine.Items.Add("流量-扬程曲线");
			this.comboBoxLine.Items.Add("流量-功率曲线");
			this.comboBoxLine.Items.Add("流量-效率曲线");
			this.comboBoxLine.Items.Add("流量-NPSH曲线");
			this.comboBoxLine.SelectedIndex = 0;
		}

		private void UpdataChartData()
		{
			switch (this.comboBoxLine.SelectedIndex)
			{
			case 0:
				this.hyPumpGraphChart.Draw(this.pumpdata, EPumpCurveType.ePumpCurve_H);
				break;
			case 1:
				this.hyPumpGraphChart.Draw(this.pumpdata, EPumpCurveType.ePumpCurve_N);
				break;
			case 2:
				this.hyPumpGraphChart.Draw(this.pumpdata, EPumpCurveType.ePumpCurve_E);
				break;
			case 3:
				this.hyPumpGraphChart.Draw(this.pumpdata, EPumpCurveType.ePumpCurve_NPSH);
				break;
			}
			this.hyPumpGraphChart.SetMarker(this.checkBoxPoint.Checked);
			this.hyPumpGraphChart.ShowXYTitle(this.checkBoxTitle.Checked);
		}

		private List<HYPumpData> listPumpData = new List<HYPumpData>();

		private List<HYPumpFactory> listPumpFac = new List<HYPumpFactory>();

		public HYPumpData pumpdata = new HYPumpData();

		public List<HYPumpData> m_arrSelPumps;
	}
}
